import 'package:flutter/foundation.dart';

//简化版的ChangeNotifier,用于数据被改变时通知监听者更新UI
//author:liuhaichao
abstract class MyChangeNotifier implements Listenable {
  final ObserverList<VoidCallback> _listeners = ObserverList<VoidCallback>();

  @protected
  bool get hasListeners {
    return _listeners.isNotEmpty;
  }

  @override
  void addListener(VoidCallback listener) {
    _listeners.add(listener);
  }

  @override
  void removeListener(VoidCallback listener) {
    _listeners.remove(listener);
  }

  @protected
  @visibleForTesting
  void notifyListeners() {
    if (_listeners.isNotEmpty) {
      final List<VoidCallback> localListeners = List<VoidCallback>.from(_listeners);
      for (final VoidCallback listener in localListeners) {
        listener();
      }
    }
  }

  bool shouldNotify<T extends MyChangeNotifier>(T t);
}
